﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MJHDataService;

public partial class assets_controls_HeightControl : System.Web.UI.UserControl
{
    private int measurementUnitID;
    public int MeasurementUnitID
    {
        get
        {
            return measurementUnitID;
        }
        set
        {
            measurementUnitID = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        using (Measurement oMeasurement = new Measurement())
        {
            Hashtable oParam = new Hashtable();
            oParam.Add("MeasurementTypeID", 1);
            oParam.Add("MinLevel", 1);
            using (DataTable dt = oMeasurement.GetMeasurementUnits(oParam))
            {
                Int32 nMaxLevel = Convert.ToInt32((object)dt.Compute("max(Level)", ""));
                Int32 nMinLevel = Convert.ToInt32((object)dt.Compute("min(Level)", ""));

                nMaxLevel = nMaxLevel - nMinLevel;
                using (DataSet dsHierarchy = DataSetUtility.TransformFlatToHierarchy(dt, nMaxLevel, "MeasurementUnitID", "ParentMeasurementUnitID"))
                {
                    InitGrid(dsHierarchy);
                    rptHeightUI.DataSource = dsHierarchy;
                    rptHeightUI.DataBind();
                }
            }
        }
    }
    private void InitGrid(DataSet ds)
    {
        foreach (DataTable dt in ds.Tables)
        {
            using (DataGrid dg = new DataGrid())
            {
                dg.DataSource = dt;
                dg.DataBind();
                phMeasurementUnits.Controls.Add(dg);
            }
        }
    }
    protected void rptHeightUI_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        using (Label lbl = (Label)e.Item.FindControl("lblHeightUnit1"))
        {
            lbl.Text = Convert.ToString(drv["MeasurementUnitName"]);
        }
        using (DataView dv = drv.CreateChildView(drv.Row.Table.DataSet.Relations[0]))
        {
            if (dv.Count > 0)
            {
                using (Repeater rpt = (Repeater)e.Item.FindControl("rptHeightSubUnit"))
                {
                    rpt.DataSource = dv;
                    rpt.DataBind();
                }
            }
        }
    }
    protected void rptHeightSubUnit_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        using (Label lbl = (Label)e.Item.FindControl("lblHeightUnit2"))
        {
            lbl.Text = Convert.ToString(drv["MeasurementUnitName"]);
        }
    }
}
